package cn.ztr.ymcc.mq;

import cn.ztr.ymcc.constans.Mqcons;
import cn.ztr.ymcc.domain.MediaFile;
import cn.ztr.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSON;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RocketMQMessageListener(
        topic = Mqcons.TOPIC_COURSE_VIDEO,
        selectorExpression = "*",
        consumerGroup = "service-media-consumer"
)
public class MediaConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IMediaFileService mediaFileService;


    @Override
    public void onMessage(MessageExt messageExt) {
        String mediaJson = new String(messageExt.getBody(), CharsetUtil.UTF_8);
        log.info(mediaJson);
        MediaFile mediaFile = JSON.parseObject(mediaJson, MediaFile.class);
        //把文件转成m3u8格式,并上传到文件服务器
        mediaFileService.handleFile2m3u8(mediaFile);
        log.debug("消费者处理完成");
    }
}
